package com.icinfo.cloud.provider.common.interceptor;

import com.icinfo.cloud.provider.common.utils.BusinessContextUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 线程上下文拦截器，用于清空请求后的上下文参数
 *
 * @author Junfeng.Chen
 * @date 2023/12/11 13:18
 **/
@Configuration
public class BusinessContextInterceptor implements HandlerInterceptor, WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //增加拦截路径
        registry.addInterceptor(new BusinessContextInterceptor()).addPathPatterns("/**");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //移除上下文
        BusinessContextUtil.remove();
    }
}
